<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('业务类型')" prop="businessType">
            <dict-select
              v-model="form.businessType"
              dictionary="deal_type"
              :placeholder="$t('业务类型')"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <div class="area-header">
        <span class="area-header-title">{{ $t('风险点') }}</span>
      </div>
      <el-table
        :data="form.contentList"
        border
        class="nowrapTable formTable"
      >
        <el-table-column :label="$t('序号')" align="center" width="50">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('内容')" align="center">
          <template slot-scope="scope">
            <el-form-item :prop="'contentList.' + scope.$index + '.content'">
              <el-input v-model="scope.row.content" type="textarea" maxlength="2000" show-word-limit :autosize="{ minRows:1,maxRows:10 }" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('操作')" align="center" width="80">
          <template slot-scope="scope">
            <el-button v-hasPermi="['deal:riskSet:remove']" size="small" type="text" icon="el-icon-delete" @click="delContent(scope.$index)" />
          </template>
        </el-table-column>
      </el-table>
      <div class="tol">
        <el-button v-hasPermi="['deal:riskSet:edit']" type="primary" icon="el-icon-plus" size="mini" @click="addContent">{{ $t("添加") }}</el-button>
      </div>

    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="deal:riskSet" />
    </div>
  </div>
</template>

<script>
    import { saveRiskSet, getRiskSet } from '@/api/sass/hxs/riskSet'
    export default {
        data() {
            return {
                // 表单参数
                form: {
                  contentList: []
                },
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                businessType: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ]
                }
            }
        },
        created() {
            this.getDicts('deal_type').then(response => {
                this.businessTypeOptions = response.data
            })
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getRiskSet(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    businessType: undefined,
                    remark: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined,
                    delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveRiskSet(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                }).catch(() => {
                    this.loading = false
                })
           },
          addContent() {
            const row = {
              id: undefined,
              riskSetId: undefined,
              content: undefined,
              sort: undefined
            }
            if (!(this.form.contentList instanceof Array)) {
              this.$set(this.form, 'contentList', [])
            }
            this.$nextTick(() => {
              this.form.contentList.push(row)
            })
          },
          delContent(index) {
              this.form.contentList.splice(index, 1)
          }
        }
    }
</script>
